//
//  CRSessionTextContentView.m
//  TestPrj
//
//  Created by 朱铅峰 on 2020/10/21.
//  Copyright © 2020 Xing. All rights reserved.
//

#import "CRSessionTextContentView.h"
#import "M80AttributedLabel+CRKit.h"
#import "CRMessageModel.h"
#import "CRKit.h"

@interface CRSessionTextContentView()<M80AttributedLabelDelegate>

@end

@implementation CRSessionTextContentView

- (instancetype)initSessionMessageContentView {
    
    if (self = [super initSessionMessageContentView]) {
        
        _textLabel = [[M80AttributedLabel alloc] initWithFrame:CGRectZero];
        _textLabel.delegate = self;
        _textLabel.numberOfLines = 0;
        _textLabel.autoDetectLinks = NO;
        _textLabel.lineBreakMode = NSLineBreakByWordWrapping;
        _textLabel.backgroundColor = [UIColor clearColor];
        [self addSubview:_textLabel];
    }
    return self;
}

- (void)refresh:(CRMessageModel *)data {
    
    [super refresh:data];
    NSString *text = self.model.message.text;
    CRKitSetting *setting = [[CRKit sharedKit].config setting:data.message];

    self.textLabel.textColor = setting.textColor;
    self.textLabel.font = setting.font;
    [self.textLabel cr_setText:text];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    UIEdgeInsets contentInsets = self.model.contentViewInsets;
    
    CGFloat tableViewWidth = self.superview.width;
    CGSize contentsize         = [self.model contentSize:tableViewWidth];
    CGRect labelFrame = CGRectMake(contentInsets.left, contentInsets.top, contentsize.width, contentsize.height);
    self.textLabel.frame = labelFrame;
}


#pragma mark - M80AttributedLabelDelegate
- (void)m80AttributedLabel:(M80AttributedLabel *)label
             clickedOnLink:(id)linkData {
    
    CRKitEvent *event = [[CRKitEvent alloc] init];
    event.eventName = CRKitEventNameTapLabelLink;
    event.messageModel = self.model;
    event.data = linkData;
    [self.delegate onCatchEvent:event];
}


@end
